﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;

namespace Financial
{
    class Industry
    {
        public Industry() { }
        public Industry(String URL, String name) 
        {
            this.URL = URL;
            this.name = name;
        }

        public String GetIndustrySource(String URL)
        {
            HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(URL);
            HttpWebResponse webresp = (HttpWebResponse)webreq.GetResponse();
            StreamReader strm = new StreamReader(webresp.GetResponseStream(), Encoding.ASCII);
            return strm.ReadToEnd();
        }

        public void GetIndustryFromSource(String source)
        {
            String str = Regex.Split(source, "Sector</span><ul><li><a href=\"")[1];
            String data = Regex.Split(str, "</a></li></ul></li>")[0];
            String[] members = Regex.Split(data, "\">");
            this.URL = members[0];
            this.name = members[1];
        }

        public void GetIndustryPropertiesFromSource(String source)
        {
            String str = Regex.Split(source, "<b>Sector:</b>")[1];
            String data = Regex.Split(str, "<b>Industries</b>")[0];
            String finalData = Regex.Split(data, "\n" + this.name + "</font></td>")[1];
            finalData = finalData.Replace('\n', ' ');
            String[] parse = Regex.Split(
                            finalData, "<td align=right bgcolor=FFFFFF><font face=arial size=-1>");
            CopyToProperties(parse);
        }

        private void CopyToProperties(String[] data)
        {
            One_Day_Price_Change = Regex.Split(data[1], "</font></td>")[0];
            Market_Capitalization = Regex.Split(data[2], "</font></td>")[0];
            PE_Ratio = Regex.Split(data[3], "</font></td>")[0];
            ROE = Regex.Split(data[4], "</font></td>")[0];
            Dividend_Yield = Regex.Split(data[5], "</font></td>")[0];
            Long_Term_Debt_To_Equity = Regex.Split(data[6], "</font></td>")[0];
            Price_To_Book_Value = Regex.Split(data[7], "</font></td>")[0];
            Net_Profit_Margin_Percent = Regex.Split(data[8], "</font></td>")[0];
            Price_To_Free_Cash_Flow = Regex.Split(data[9], "</font></td>")[0];
        }

        public String URL { set; get; }
        public String name { set; get; }
        public String One_Day_Price_Change { set; get; }
        public String Market_Capitalization { set; get; }
        public String PE_Ratio { set; get; }
        public String ROE { set; get; }
        public String Dividend_Yield { set; get; }
        public String Long_Term_Debt_To_Equity { set; get; }
        public String Price_To_Book_Value { set; get; }
        public String Net_Profit_Margin_Percent { set; get; }
        public String Price_To_Free_Cash_Flow { set; get; }
        public String More_Information { set; get; }
    }
}
